home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
cpp_libs
/
awe2-0_1.lha
/
awe2-0.1
/
Src
/
RCS
/
GenericKeyedHeap.h,v
< prev
next >
Wrap
Text File
|
1988-10-12
|
4KB
|
160 lines
head 1.1;
access ;
symbols ;
locks grunwald:1.1; strict;
comment @ * @;
1.1
date 88.09.18.16.42.06; author grunwald; state Exp;
branches ;
next ;
desc
@@
1.1
log
@Initial revision
@
text
@//
// Define the following things:
// HEAP_NAME -- the name of the heap type
// HEAP_INDEX -- the index type; defaults to unsigned short
// HEAP_KEY -- the key type; defaults to double
// HEAP_ITEM -- the type name for an item; should be typedefed
// HEAP_INDEX_NULL -- the null value for the heap index; defaults to 65535
// HEAP_BASE_CLASS -- the base class for the new heap
//
#include "Awesime.h"
#include "Generic.h"
#ifndef HEAP_INDEX
#define HEAP_INDEX unsigned short
#endif
#ifndef HEAP_INDEX_NULL
#define HEAP_INDEX_NULL 0xffff
#endif
#ifndef HEAP_KEY
#define HEAP_KEY double
#endif
#ifndef HEAP_BASE_CLASS
#define HEAP_BASE_CLASS public Awesime
#endif
typedef HEAP_ITEM GENERIC2(HEAP_NAME,Item);
typedef HEAP_KEY GENERIC2(HEAP_NAME,Key);
typedef HEAP_INDEX GENERIC2(HEAP_NAME,Index);
extern const GENERIC2(HEAP_NAME,Index) GENERIC2(HEAP_NAME,Null);
typedef struct {
GENERIC2(HEAP_NAME,Key) key;
GENERIC2(HEAP_NAME,Item) item;
GENERIC2(HEAP_NAME,Index) sibling;
GENERIC2(HEAP_NAME,Index) children;
} GENERIC2(HEAP_NAME,Record);
class HEAP_NAME : HEAP_BASE_CLASS {
GENERIC2(HEAP_NAME,Record) *storage;
char *pValid;
int elements;
GENERIC2(HEAP_NAME,Index) allocatedSize;
GENERIC2(HEAP_NAME,Index) freelist;
GENERIC2(HEAP_NAME,Index) root;
void makeRoom(int atLeast);
GENERIC2(HEAP_NAME,Index) makeChild(GENERIC2(HEAP_NAME,Index) a, GENERIC2(HEAP_NAME,Index) b);
inline GENERIC2(HEAP_NAME,Index) getCell() {
if (freelist == GENERIC2(HEAP_NAME,Null)) {
makeRoom(0);
}
GENERIC2(HEAP_NAME,Index) cell = freelist;
pValid[freelist] = 1;
freelist = storage[freelist].sibling;
elements++;
return(cell);
}
inline void returnCell(GENERIC2(HEAP_NAME,Index) cell) {
storage[cell].sibling = freelist;
pValid[cell] = 0;
freelist = cell;
elements--;
}
public:
HEAP_NAME(int length = 0, bool xdebug = 0);
void add(GENERIC2(HEAP_NAME,Key) *key, GENERIC2(HEAP_NAME,Item) *item);
bool remove(GENERIC2(HEAP_NAME,Key) *key,
GENERIC2(HEAP_NAME,Item) *removed);
//
// The next four members allow you to search a HEAP_NAME and mark
// items as invalid.
//
// doStart initilizes the index and returns the first item in the list.
// doNext moves to the next item and returns that item.
// Both return a bool indicating whether the value in item has any
// meaning. When bool=FALSE, you've searched everything.
// Call doDone at the end to insure compatibility with subclasses.
//
virtual bool doStart( GENERIC2(HEAP_NAME,Index) &index);
virtual bool doDelete(GENERIC2(HEAP_NAME,Index) &index);
virtual bool doNext( GENERIC2(HEAP_NAME,Index) &index);
virtual void doDone();
inline GENERIC2(HEAP_NAME,Index) minItem() {
return(root);
};
inline bool valid(GENERIC2(HEAP_NAME,Index) index) {
return ( pValid[index] );
}
inline GENERIC2(HEAP_NAME,Key) & key(GENERIC2(HEAP_NAME,Index) i) {
return( storage[i].key );
}
inline GENERIC2(HEAP_NAME,Item) & item(GENERIC2(HEAP_NAME,Index) i) {
return( storage[i].item );
}
inline GENERIC2(HEAP_NAME,Index) maxIndex() {
return(allocatedSize);
}
inline bool isEmpty() {
return(elements <= 0);
}
inline unsigned int size() {
return(elements);
}
virtual void classPrintOn(ostream& s);
};
#undef HEAP_NAME
#undef HEAP_ITEM
#undef HEAP_KEY
#undef HEAP_INDEX
#undef HEAP_BASE_CLASS
@